<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>有奖答题平台</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
    <link rel="stylesheet" href="/mobile/css/bui.css" />
    <link rel="stylesheet" href="/lib/mobile/css/common.css?000"/>
    <link rel="stylesheet" href="/lib/mobile/css/coupon.css?1000"/>
</head>
<body class="coupon_body" style="height: 100vh;">
    <div class="coupon">
        <div class="coupon_title">智慧币换礼</div>
        <div class="coupon_money">我的<strong id="totalmoney"></strong>智慧币</div>
        <div class="coupon_record"><s>点击以下商品可兑换 ↓</s><a href="javascript:void(0)" id="coupon_record">我的兑换记录</a></div>

        <div id="ywclist" class="coupon_list coupon_list_4 bui-scroll">
            <div class="bui-scroll-head"></div>
            <div class="bui-scroll-main">
                <ul class="bui-list bui-list-thumbnail" id="ddlb">

                </ul>
            </div>
            <div class="bui-scroll-foot"></div>
        </div>
        <ul class="bottom_menu">
            <li  id="indexsy"><a href="javascript:void(0);">首页</a></li>
            <!--<li>|</li>-->
            <li  id="hdrule"><a href="javascript:void(0);">活动规则</a></li>
            <!--<li  id="point_rule"><a href="javascript:void(0);">积分规则</a></li>-->
            <!--<li>|</li>-->
            <!--<li id="prize_rule"><a href="javascript:void(0);">抽奖规则</a></li>-->
            <!--<li>|</li>-->
            <li id="center"><a href="javascript:void(0);">个人中心</a></li>
        </ul>
    </div>
    <!--<div id="dialogDown" class="bui-dialog" style="display: none;">-->
        <!--<div class="bui-dialog-head">智慧币换礼</div>-->
        <!--<div class="bui-dialog-main">-->
            <!--&lt;!&ndash;<img src="images/placeholder-page.png" alt="占位图">&ndash;&gt;-->
        <!--</div>-->
        <!--<div class="bui-dialog-foot">-->
            <!--<div class="bui-box">-->
                <!--<div class="span1">-->
                    <!--<div class="bui-btn">取消</div>-->
                <!--</div>-->
                <!--<div class="span1">-->
                    <!--<div class="bui-btn blue">确定</div>-->
                <!--</div>-->
            <!--</div>-->
        <!--</div>-->
        <!--<div class="bui-dialog-close"><i class="icon-close"></i></div>-->
    <!--</div>-->
    <!-- 依赖库 手机调试的js引用顺序如下 -->
    <script src="/lib/mobile/js/zepto.js"></script>
    <script src="/lib/mobile/js/plugins/fastclick.js"></script>
    <script src="/lib/mobile/js/common.js?0000    "></script>
    <script src="/lib/mobile/js/bui.js"></script>
    <script>
        bui.ready(function () {
            // 这里写业务及控件初始化
            var storage = bui.storage();
            var uid=storage.get("uid");

            bui.btn({id: ".bui-page", handle: ".bui-btn"}).load();
            var pageview = {};
            pageview.init = function () {
                $("#coupon_record").on("click", function (argument) {
                    bui.load({ url: BASE_URL+"mobile/coupon_money?uid="+uid[0]});
                });
                // 验证码示例
                //我的积分
                getCouponList(uid[0]);
                getTotalmoney(uid[0]);
            }
            pageview.init();

        })
        function getCouponList(uid){
            bui.list({
                id: "#ywclist",
                url: BASE_URL+"mobile/getCouponList",
                pageSize: 4,
                data: {uid:uid},
                field: {
                    page: "page",
                    size: "pageSize",
                    data: "data"
                },
                callback: function(e) {
                    // e.target 为你当前点击的元素
                    // e.currentTarget 为你当前点击的handle 整行
                },
                template: function(user) {
                    var html = "";
                    // console.log(user);
                    // var user = res.data;
                    // $("#ddlb").html("");
                    for(var i=0;i<user.length;i++){
                        // console.log(user[i].pic);
                        html+="<li><dl><img src=\""+user[i].pic+"\" alt=\"\" /><div class=\"coupon_tips\" onclick='addDayExchange("+uid+","+user[i].shopid+")'>点击兑换</div><i><s>价格:"+user[i].price+"智慧币</s><b>"+user[i].sname+"</b></i></dl></li>";
                        // html+="<li onclick='addDayExchange("+uid+","+user[i].shopid+")'><dl><img src=\""+user[i].pic+"\" alt=\"\" />" +
                        //     "<div class=\"coupon_tips\">"+user[i].sname+"</div><i><s>"+user[i].price+"智慧币</s><b></b></i></dl></li>";
                    }
                    return html;
                },
                onBeforeRefresh: function() {
                    // console.log("brefore refresh")
                },
                onBeforeLoad: function() {
                    // console.log("brefore load")
                },
                onRefresh: function() {
                    // 刷新以后执行
                    // console.log("refreshed")
                },
                onLoad: function() {
                    // 刷新以后执行
                    // console.log("loaded")
                }
            })
        }
        function getTotalmoney(uid) {
            //执行登录操作
            // console.log($("#"+objectid));
            var loginAjax = bui.ajax({
                url: BASE_URL+"mobile/getTotalmoney",
                // 可选参数
                data: {
                    uid:uid
                },
                method: "POST"
            });
            loginAjax.then(function(res) {
                if(res.code == 0){
                    $("#totalmoney").html(res.data.totalmoney);
                }else{
                    bui.alert(res.msg);
                }
            },function(res) {
                bui.alert("请求异常");
            })
        }
        // var uiDialogRight = bui.dialog({
        //     id: "#dialogDown",
        //     effect: "fadeInRight",
        //     position: "bottom",
        //     width: "100%",
        //     fullscreen: false,
        //     style: {
        //         left: "auto"
        //     },
        //     callback: function(e) {
        //         console.log(e.target)
        //         bui.alert("已取消兌換！");
        //     }
        // });
        function addDayExchange(uid,shopid) {
            // console.log(uid+"=========="+shopid);
            //执行登录操作
            bui.confirm({
                content:"确定兑换商品吗？",
                title:"智慧币换礼",
                buttons:["取消","确定"],
                callback:function(e){
                    var text = $(e.target).text();
                    if( text == "确定"){
                        var loginAjax = bui.ajax({
                            url: BASE_URL+"mobile/addDayExchange",
                            // 可选参数
                            data: {
                                uid:uid,
                                shopid:shopid
                            },
                            method: "POST"
                        });
                        loginAjax.then(function(res) {
                            if(res.code == 0){
                                getTotalmoney(uid);
                                bui.alert(res.msg);
                            }else{
                                // console.log(res)
                                bui.alert(res.msg);
                                // bui.alert(res.msg);
                            }
                        },function(res) {
                            bui.alert("请求异常");
                        })
                    }else{
                        bui.alert("已取消兌換！");
                    }
                    this.close();
                }
            })

        }

        function imgHeight(obj){
            // console.log($(obj).find("li").eq(0).find("img").height());
            var imgheight = $(obj).find("li").eq(0).find("img").height();
            imgheight = 500;
            $(obj).find("li").each(function(){
                $(this).find("img").height(imgheight);
            })
        }
    </script>
</body>
</html>